package com.example.redis.api;


import com.example.redis.api.param.BatchIdParam;
import com.example.redis.api.param.GoodsEditParam;
import com.example.redis.api.param.GoodsParam;
import com.example.redis.config.ServiceResultEnum;
import com.example.redis.entity.Goods;
import com.example.redis.service.GoodsService;
import com.example.redis.util.BeanUtil;
import com.example.redis.util.Result;
import com.example.redis.util.ResultGenerator;
import com.example.redis.util.PageQueryUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "goods",tags = "测试接口")
@RequestMapping("/api/goods")
public class GoodsAPI {
    private static final Logger logger = LoggerFactory.getLogger(GoodsAPI.class);
    @Resource
    GoodsService goodsService;
    @GetMapping("/search")
    @ApiOperation(value = "商品列表", notes = "商品列表")
    public Result list(@RequestParam(required = false) @ApiParam(value = "页码") Integer pageNumber,
                       @RequestParam(required = false) @ApiParam(value = "每页条数") Integer pageSize
                      ) {
        if (pageNumber == null || pageNumber < 1 || pageSize == null || pageSize < 4) {
            return ResultGenerator.genFailResult("分页参数异常！");
        }
        Map params = new HashMap(4);
        params.put("page", pageNumber);
        params.put("limit", pageSize);
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(goodsService.getGoodsPage(pageUtil));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除记录", notes = "删除记录")
    public Result Info(@RequestBody BatchIdParam batchIdParam) {
        if (batchIdParam == null || batchIdParam.getIds().length < 1) {
            return ResultGenerator.genFailResult("参数异常");
        }

        if (goodsService.deleteBatch(batchIdParam.getIds())) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("参数异常");
        }
    }
    @PostMapping("/insert")
    @ApiOperation(value = "插入一条新记录", notes = "插入一条新记录")
    public Result save(@RequestBody @Validated GoodsParam goodsParam){
        Goods goods = new Goods();
        BeanUtil.copyProperties(goodsParam, goods);
        String result = goodsService.saveGoods(goods);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }
    /**
     * 更新
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新一条记录", notes = "更新一条新纪录")
    public Result update(@RequestBody @Validated GoodsEditParam goodsEditParam){
        Goods goods = new Goods();
        BeanUtil.copyProperties(goodsEditParam, goods);
        String result = goodsService.updateGoods(goods);
        if(ServiceResultEnum.SUCCESS.getResult().equals(result)){
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    @GetMapping("/getById")
    @ApiOperation(value = "根据id查询商品信息", notes = "根据id查询商品信息")
    public Goods getById(String id){
        return goodsService.getById(Long.valueOf(id));
    }

    @GetMapping("/first")
    @ApiOperation(value = "查询底部商品", notes = "查询底部商品")
    public List<Goods> getFirstGoods(){
        return goodsService.getFirstGoods();
    }
}
